Masthead

Structuring Folders of Code

1. Introduction

When you start creating more software, including modules and even your own packages, you'll want to design how the code files will be stored. I recommend breaking up your files into two main folders: "Projects" and "Reusable". The idea is that the "Projects" folder will contain a folder for each project you are working on. Then the "Reusable" file will contain the classes you create that are reusable between projects. The final structure might look like:

The "Utilities" folder then might contain a "DateUtil.py" module, a "CoordinateUtil.py", etc. "FileHandlers" might contain modules that deal with specific file formats while "WebServices" contains modules for web services and so on. The "Projects" folder then contains a folder for each project you work on.

2. Specifying the Location of Modules

When you "import" a module or package, Python looks in the folder where the script is stored that is being run and looks at packages that are installed. Python will also look in other folders that are specified by the system "path". You can modify this path outside of Python or you can add the code below to scripts that use modules in other folders. You'll want to add a line to "append" the path to each reusable code folder that is used in a script file.

import sys  
sys.path.append("C:/ProjectsPython/Reusable/Utilities") 

Note: Packages are installed in a folder called "site-packages" inside the PythonXX folder where XX is the version number of Python. You should really not copy packages into the folder yourself but let the package installers do this for you.

Additional Resources:

Forum on importing modules

© Copyright 2018 HSU - All rights reserved.